package com.tensquare.base.controller;

import com.tensquare.base.pojo.Label;
import com.tensquare.base.service.LabelService;
import dto.PageResultDTO;
import dto.ResultDTO;
import dto.StatusCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.annotation.RequestScope;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@CrossOrigin
@RequestMapping("/label")
public class LabelController {
    @Autowired
    private LabelService labelService;

    /**
     * 查询所有
     *
     * @return
     */
    @GetMapping
    public ResultDTO findAll() {
        // 带data数据到有参构造
        return new ResultDTO(true, StatusCode.OK, "查询成功", this.labelService.findAll());
    }

    /**
     * 根据id查询
     */
    @GetMapping("/{labelId}")
    public ResultDTO findById(@PathVariable(name = "labelId") String id) {
        // 带data数据到有参构造
        return new ResultDTO(true, StatusCode.OK, "查询成功", this.labelService.findById(id));
    }

    /**
     * 添加成功
     */
    @RequestMapping(method = RequestMethod.POST)
    public ResultDTO save(@RequestBody Label label) {
        this.labelService.save(label);
        // 不带data数据带有参构造
        return new ResultDTO(true, StatusCode.OK, "添加成功");
    }

    /**
     * 更新数据
     */
    @RequestMapping(value = "/{labelId}", method = RequestMethod.PUT)
    public ResultDTO update(@PathVariable(name = "labelId") String labelId, @RequestBody Label label) {
        // 给生成带Label添加id属性
        label.setId(labelId);

        this.labelService.update(label);

        // 不带data数据带有参构造
        return new ResultDTO(true, StatusCode.OK, "更新成功");
    }

    /**
     * 删除数据
     */
    @RequestMapping(value = "/{labelId}", method = RequestMethod.DELETE)
    public ResultDTO delete(@PathVariable(name = "labelId") String labelId) {
        this.labelService.delete(labelId);
        return new ResultDTO(true, StatusCode.OK, "删除成功");
    }

    /**
     * 根据条件查询列表
     */
    @RequestMapping(value = "/search", method = RequestMethod.POST)
    public ResultDTO findLabelList(@RequestBody Label label) {
        List<Label> list = this.labelService.findLabelList(label);
        return new ResultDTO(true, StatusCode.OK, "查询成功", list);
    }

    /**
     * 根据条件分页查询
     */
    @RequestMapping(value = "/search/{page}/{size}", method = RequestMethod.POST)
    public ResultDTO pageQuery(@RequestBody Label label, @PathVariable int page, @PathVariable int size) {
        Page<Label> pageResult = this.labelService.pageQuery(label, page, size);
        // 返回自己封装的分页对象
        return new ResultDTO(true, StatusCode.OK, "查询成功", new PageResultDTO<Label>(pageResult.getTotalElements(), pageResult.getContent()));
    }
}
